package cn.edu.zafu.bigdata.x.Foo;

import java.io.*;
import java.util.*;

// 学生类
class Student implements Serializable {
    private static final long serialVersionUID = 1L;
    private String id;
    private String name;
    private String phone;

    public Student(String id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }

    // Getters and setters
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

public class StudentManagementSystem {
    private static final String FILENAME = "students.ser";
    private List<Student> students = new ArrayList<>();
    private Scanner scanner;

    public static void main(String[] args) {
        StudentManagementSystem sms = new StudentManagementSystem();
        sms.run();
    }

    public void run() {
        scanner = new Scanner(System.in);
        loadStudents();
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // consume newline

            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    findStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    deleteStudent();
                    break;
                case 5:
                    saveStudents();
                    System.out.println("程序退出。");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效的选项，请重新输入。");
                    break;
            }
        }
    }

    private void addStudent() {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        for (Student s : students) {
            if (s.getId().equals(id)) {
                System.out.println("学号重复，请重新输入。");
                return;
            }
        }
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();
        Student student = new Student(id, name, phone);
        students.add(student);
        saveStudents();
        System.out.println("学生信息添加成功。");
    }

    private void findStudent() {
        while (true) {
            System.out.print("请输入学号 (输入'QUIT'退出): ");
            String input = scanner.nextLine();
            if ("QUIT".equals(input)) break;
            Optional<Student> studentOptional = students.stream()
                    .filter(s -> s.getId().equals(input))
                    .findFirst();
            studentOptional.ifPresentOrElse(
                    student -> System.out.println(student),
                    () -> System.out.println("未找到该学生。")
            );
        }
    }

    private void updateStudent() {
        while (true) {
            System.out.print("请输入学号 (输入'QUIT'退出): ");
            String input = scanner.nextLine();
            if ("QUIT".equals(input)) break;
            Student student = students.stream()
                    .filter(s -> s.getId().equals(input))
                    .findFirst()
                    .orElse(null);
            if (student == null) {
                System.out.println("未找到该学生。");
            } else {
                System.out.print("请输入新的姓名: ");
                String name = scanner.nextLine();
                System.out.print("请输入新的电话: ");
                String phone = scanner.nextLine();
                student.setName(name);
                student.setPhone(phone);
                saveStudents();
                System.out.println("学生信息更新成功。");
            }
        }
    }

    private void deleteStudent() {
        while (true) {
            System.out.print("请输入学号 (输入'QUIT'退出): ");
            String input = scanner.nextLine();
            if ("QUIT".equals(input)) break;
            boolean removed = students.removeIf(s -> s.getId().equals(input));
            if (removed) {
                saveStudents();
                System.out.println("学生信息删除成功。");
            } else {
                System.out.println("未找到该学生。");
            }
        }
    }

    private void loadStudents() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILENAME))) {
            students = (List<Student>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("无法加载学生信息，将创建新的列表。");
        }
    }

    private void saveStudents() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILENAME))) {
            oos.writeObject(students);
        } catch (IOException e) {
            System.out.println("无法保存学生信息。");
        }
    }
}



